package com.eqdd.yiqidian.ui.mine;

import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;

import com.eqdd.yiqidian.R;
import com.eqdd.yiqidian.RecyclerViewCustom;
import com.eqdd.yiqidian.base.Config;
import com.eqdd.yiqidian.bean.rebuild.OptionBean;
import com.eqdd.yiqidian.bean.rebuild.slimedit.SlimEditChooseBean;
import com.eqdd.yiqidian.bean.rebuild.slimedit.SlimEditEnterBean;
import com.eqdd.yiqidian.bean.rebuild.slimedit.SlimEditTimeBean;
import com.eqdd.yiqidian.http.DialogCallBack;
import com.eqdd.yiqidian.http.HttpConfig;
import com.eqdd.yiqidian.http.HttpResult;
import com.eqdd.yiqidian.ui.IndustryTypeListActivity;
import com.eqdd.yiqidian.ui.base.SlimBaseMultiEditActivity;
import com.eqdd.yiqidian.utils.SingleChooseUtil;
import com.eqdd.yiqidian.utils.ToastUtil;
import com.eqdd.yiqidian.widget.ActionSheetDialog;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;

/**
 * Created by 吕志豪 on 17-10-12  上午10:21.
 * Github :https://github.com/lvzhihao100
 * E-Mail：1030753080@qq.com
 * 简书 :http://www.jianshu.com/u/6e525b929aac
 */

public class AddWorkExpActivity extends SlimBaseMultiEditActivity {


    private RecyclerViewCustom dataBinding;
    private ActionSheetDialog chooseGuimo;
    private ActionSheetDialog chooseXingzhi;
    private ActionSheetDialog chooseXinzi;
    private String code;
    private String msg;

    @Override
    public void initBinding() {
        dataBinding = DataBindingUtil.setContentView(this, R.layout.activity_recyclerview);
        initTopTitleBar(View.VISIBLE, "添加工作经历");
        initTopRightText("提交", v -> {
            if (checkParam()) {
                OkGo.<HttpResult>post(HttpConfig.BASE_URL + HttpConfig.ADD_WORK_EXP)
                        .params("userGuid", user.getGuid())
                        .params("startTime", setParam(7))
                        .params("endTime", setParam(8))
                        .params("company", setParam(0))
                        .params("induCategoryCode", code)
                        .params("induCategoryName", setParam(6))
                        .params("enterpriseNature", setParam(2))
                        .params("enterpriseScale", setParam(1))
                        .params("monthlySalary", setParam(5))
                        .params("department", setParam(3))
                        .params("post", setParam(4))
                        .params("jobDescri", setParam(9))
                        .params("isOvert", setParam(10).equals("不公开") ? 0 : 1)
                        .execute(new DialogCallBack<HttpResult>(AddWorkExpActivity.this) {
                            @Override
                            public void onSuccess(Response<HttpResult> response) {
                                HttpResult httpResult = response.body();
                                ToastUtil.showShort(httpResult.getMsg());
                                if (httpResult.getStatus() == 200) {
                                    setResult(Config.SUCCESS);
                                    finish();
                                }
                            }
                        });
            } else {
                ToastUtil.showShort(msg);
            }
        });
    }

    private boolean checkParam() {
        for (int i = 0; i < 10; i++) {
            if (TextUtils.isEmpty(setParam(i))) {
                msg = "请补全所有信息";
                return false;
            }
        }
        return true;
    }

    @Override
    protected RecyclerView initRecyclerView() {
        return dataBinding.recyclerView;
    }

    @Override
    public void itemClick(int p) {

        switch (slimAdapterEx.getDataItem(p).getPosition()) {
            case 1:
                if (chooseGuimo == null) {
                    SingleChooseUtil.getChooseDialog(AddWorkExpActivity.this, 4, (sheet) -> {
                        chooseGuimo = sheet;
                        sheet.show();
                    }, (position, strings) -> {
                        slimAdapterEx.getDataItem(1).setContent(strings.get(position - 1));
                        slimAdapterEx.notifyItemChanged(1);
                    });
                } else {
                    chooseGuimo.show();
                }
                break;
            case 2:
                if (chooseXingzhi == null) {
                    SingleChooseUtil.getChooseDialog(AddWorkExpActivity.this, 6, (sheet) -> {
                        chooseXingzhi = sheet;
                        sheet.show();
                    }, (position, strings) -> {
                        slimAdapterEx.getDataItem(2).setContent(strings.get(position - 1));
                        slimAdapterEx.notifyItemChanged(2);
                    });
                } else {
                    chooseXingzhi.show();
                }
                break;
            case 5:
                if (chooseXinzi == null) {
                    SingleChooseUtil.getChooseDialog(AddWorkExpActivity.this, 36, (sheet) -> {
                        chooseXinzi = sheet;
                        sheet.show();
                    }, (position, strings) -> {
                        slimAdapterEx.getDataItem(5).setContent(strings.get(position - 1));
                        slimAdapterEx.notifyItemChanged(5);
                    });
                } else {
                    chooseXinzi.show();
                }
                break;
            case 6:
                Intent intent = new Intent(AddWorkExpActivity.this, IndustryTypeListActivity.class);
                intent.putExtra(Config.TYPE, 2);
                intent.putExtra(Config.TITLE, "工作行业");
                startActivityForResult(intent, 6);
                break;

        }
    }

    @Override
    protected void setEditBeans() {

        datas.add(new SlimEditEnterBean("企业名称", "", 0, "请填写"));
        datas.add(new SlimEditEnterBean("企业规模", "", 1, true, "请选择"));
        datas.add(new SlimEditEnterBean("企业性质", "", 2, true, "请选择"));
        datas.add(new SlimEditEnterBean("所在部门", "", 3, "请填写"));
        datas.add(new SlimEditEnterBean("担任职位", "", 4, "请填写"));
        datas.add(new SlimEditEnterBean("薪资范围", "", 5, true, "请选择"));

        datas.add(new SlimEditEnterBean("工作行业", "", 6, true, "请选择"));
        datas.add(new SlimEditTimeBean("工作开始时间", "", 7, "请选择").setType(Config.YEAR_MONTH_DAY_TYPE));
        datas.add(new SlimEditTimeBean("工作结束时间", "", 8, "请选择").setType(Config.YEAR_MONTH_DAY_TYPE));
        datas.add(new SlimEditEnterBean("工作描述", "", 9, "请填写"));
        datas.add(new SlimEditChooseBean("是否公开", "", 10, "请选择", new String[]{"不公开", "公开"}));

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 6 && resultCode == RESULT_OK) {
            OptionBean bean = (OptionBean) data.getSerializableExtra("data");
            if (bean != null) {
                code = bean.getCode();
                slimAdapterEx.getDataItem(6).setContent(bean.getName());
                slimAdapterEx.notifyItemChanged(6);
            }
        }
    }
}
